<?php

namespace app\api\controller\shop;


use app\admin\model\Roomclean;
use app\admin\model\Shopstaff;
use app\common\controller\Api;

/**
 *
 */
class Staff extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * 员工列表
     *
     */
    public function getList()
    {
        $shop_id =  $this->request->param('shop_id');
        $role = $this->request->param('role');
        $where = array();
        $where['shop_id'] = $shop_id;
        if($role != ''){
            $where['role'] = $role;

        }
        $shopStaffModel = new Shopstaff();
        $list = $shopStaffModel->where($where)->select();
        $this->success('请求成功',$list);
    }

    /**
     * 添加员工
     */
    public function addStaff(){
        $user = $this->auth->getUser();
        $shop_id =  $this->request->param('shop_id');
        $mobile = $this->request->param('mobile');
        $username = $this->request->param('username');
        $role =  $this->request->param('role');
        $shopModel = new \app\admin\model\Shop();
        $shopStaffModel = new Shopstaff();
        $shop = $shopModel->where('id',$shop_id)->find();
        if($user['id'] != $shop['user_id']){
            $this->error("您无权限添加");
        }

        $userModel = new \app\admin\model\User();
        $regUser = $userModel->where('mobile',$mobile)->find();
        if(!$regUser){//注册
            $db = array();
            $db['group_id'] = 1;
            $db['username'] = "微信用户";
            $db['mobile'] = $mobile;
            $db['createtime'] = time();
            $db['status'] = 'normal';
            $uid = $userModel->insertGetId($db);
        }else{
            $uid = $regUser['id'];
        }

        $staff = $shopStaffModel->where('mobile',$mobile)->where('shop_id',$shop_id)->find();
        if(!$staff){

            $shopStaffModel->insert(['mobile'=>$mobile,'shop_id'=>$shop_id,'username'=>$username,'create_time'=>time(),
                'role'=>$role,'user_id'=>$uid]);
        }
        $this->success('成功');
    }

    public function deleteStaff(){

        $user = $this->auth->getUser();

        $shop_id =  $this->request->param('shop_id');
        $mobile = $this->request->param('mobile');
        $shopModel = new \app\admin\model\Shop();
        $shop = $shopModel->where('id',$shop_id)->find();
        if($user['id'] != $shop['user_id']){
            $this->error("您无权限添加");
        }

        $staffModel = new Shopstaff();
        $staff = $staffModel->where('shop_id',$shop_id)->where('mobile',$mobile)->find();
        if($staff){
            $staffModel->where('id',$staff['id'])->delete();
            $this->success('删除成功');
        }else{
            $this->error("您无权限添加");
        }
    }


    /**
     * 保洁记录
     */
    public function cleanList(){

        $shop_id = $this->request->param('shop_id');
        $mobile = $this->request->param('staff_mobile');
        $status = $this->request->param('status');

        $userModel = new \app\admin\model\User();
        $staff = $userModel->where('mobile',$mobile)->find();
        $page = $this->request->param('page');

        $where = array();
        $where['Roomclean.shop_id'] = $shop_id;
        if($status != ''){
            $where['Roomclean.status'] = $status;
        }
        if($mobile != ''){
            $where['user_id'] = $staff['id'];
        }

        $limit = 10;
        $offset = ($page - 1) * $limit;

        $shopCleanModel = new Roomclean();
        $list = $shopCleanModel->with(['room'])->where($where)->limit($offset,$limit)->select();
        foreach ($list as $key=>$val){
            $list[$key]['room']['logo'] = cdnurl($val['room']['logo'],true);
            $list[$key]['time'] = "";
            if($val['status'] == 'undo'){
                $minute=floor(($val['start_time']-time())%86400/60);
                $list[$key]['time'] = $minute;
            }else if( $val['status'] == 'done'){
                $minute=floor(($val['clean_end_time']-$val['clean_start_time'])%86400/60);
                $list[$key]['time'] = $minute;
            }

            $list[$key]['status_txt'] = $shopCleanModel->getStatus($val['status']);


        }
        $this->success('',$list);

    }




}
